package com.xydtech.ecard.busi.service.impl;

import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.busi.domain.EcardApiBusiAndConfig;
import com.xydtech.ecard.busi.domain.EcardApiBusiConfigList;
import com.xydtech.ecard.busi.domain.EcardApiBusiReportConfig;
import com.xydtech.ecard.busi.domain.EcardApiBusiReportPerson;
import com.xydtech.ecard.busi.mapper.EcardApiBusiAndConfigMapper;
import com.xydtech.ecard.busi.mapper.EcardApiBusiConfigListMapper;
import com.xydtech.ecard.busi.mapper.EcardApiBusiReportConfigMapper;
import com.xydtech.ecard.busi.mapper.EcardApiBusiReportPersonMapper;
import com.xydtech.ecard.busi.service.IEcardApiBusiReportPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 电子名片业务填报个人类信息Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-09-25 下午1:35
 */
@Service
public class EcardApiBusiReportPersonServiceImpl implements IEcardApiBusiReportPersonService {

    @Autowired
    private EcardApiBusiReportPersonMapper ecardBusiReportPersonMapper;
    @Autowired
    private EcardApiBusiAndConfigMapper ecardBusiAndConfigMapper;
    @Autowired
    private EcardApiBusiReportConfigMapper ecardBusiReportConfigMapper;
    @Autowired
    private EcardApiBusiConfigListMapper ecardBusiConfigListMapper;
    /**
     * 保存业务填报个人类数据接口
     * @param ecardBusiReportPerson
     * @return
     */
    @Override
    public int addReportPersonInfo(EcardApiBusiReportPerson ecardBusiReportPerson, String salt) throws Exception {
        /**
         * 1、先根据业务id查出业务表配置信息
         * 2、根据是否多类积分字段判断
         * 3、否：直接根据业务表中配置的积分方式和积分/比例字段计算
         * 4、是：根据业务id去查积分明细表在计算
         */
        String configId = ecardBusiReportPerson.getConfigId();
        EcardApiBusiReportConfig config = ecardBusiReportConfigMapper.getReportConfigById(Long.valueOf(configId));
        if (config == null) {
            throw new Exception("未配置对应积分！");
        }
        String isDpsOrLoan = config.getIsDpsOrLoan();//是否多类积分，T：是，F否
        BigDecimal points = new BigDecimal("0");//最终积分
        if ("T".equals(isDpsOrLoan) && "busi_per_deposit".equals(config.getBusiCode())){//个人类存款
            EcardApiBusiConfigList configList =  ecardBusiConfigListMapper.selectEcardBusiConfigByDps(configId,
                                                            ecardBusiReportPerson.getPerDepositType(),
                                                            ecardBusiReportPerson.getPerDepositTerm(),
                                                            ecardBusiReportPerson.getPerDepositAmount());
            int integrationWay = config.getIntegrationWay();//积分方式，1, 固定积分，2：比例提成
            Long perDepositAmount = ecardBusiReportPerson.getPerDepositAmount();//个人类存款金额
            Float nums = configList.getNums();//积分/比例
            if (integrationWay == 1){
                points = new BigDecimal(String.valueOf(nums));
            } else if (integrationWay == 2){
                //计算比例
                BigDecimal scale = new BigDecimal(String.valueOf(nums)).divide(new BigDecimal(100));
                points = new BigDecimal(perDepositAmount).multiply(scale);
            }
        }
        if ("T".equals(isDpsOrLoan) && "busi_per_loan".equals(config.getBusiCode())){//个人类贷款
            EcardApiBusiConfigList configList =  ecardBusiConfigListMapper.selectEcardBusiConfigByLoan(configId,
                    ecardBusiReportPerson.getPerLoanType(),
                    ecardBusiReportPerson.getPerLoanTerm(),
                    ecardBusiReportPerson.getPerLoanRate(),
                    ecardBusiReportPerson.getPerLoanAmount());
            int integrationWay = config.getIntegrationWay();//积分方式，1, 固定积分，2：比例提成
            Long perLoanAmount = ecardBusiReportPerson.getPerLoanAmount();//个人类贷款金额
            Float nums = configList.getNums();//积分/比例
            if (integrationWay == 1){
                points = new BigDecimal(String.valueOf(nums));
            } else if (integrationWay == 2){
                //计算比例
                BigDecimal scale = new BigDecimal(String.valueOf(nums)).divide(new BigDecimal(100));
                points = new BigDecimal(perLoanAmount).multiply(scale);
            }
        }
        if ("T".equals(isDpsOrLoan) && "busi_per_social_security".equals(config.getBusiCode())){//社保卡
            EcardApiBusiConfigList configList =  ecardBusiConfigListMapper.selectEcardBusiConfigBySocialType(configId,
                    ecardBusiReportPerson.getPerSocialType());
            Float nums = configList.getNums();//积分/比例
            points = new BigDecimal(String.valueOf(nums));

        }
        if ("F".equals(isDpsOrLoan)){//不是存/贷只有固定积分
            Float integrationNums = config.getIntegrationNums();
            points = new BigDecimal(String.valueOf(integrationNums));
        }

        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportPerson.getPerCustPhone())) {
                ecardBusiReportPerson.setPerCustPhone(AESEncryptionUtils.encrypt(ecardBusiReportPerson.getPerCustPhone(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportPerson.getPerCustIdcard())) {
                ecardBusiReportPerson.setPerCustIdcard(AESEncryptionUtils.encrypt(ecardBusiReportPerson.getPerCustIdcard(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        int i = ecardBusiReportPersonMapper.addReportPersonInfo(ecardBusiReportPerson);
        List<EcardApiBusiAndConfig> ecardBusiAndConfigList = new ArrayList<>();
        EcardApiBusiAndConfig ecardBusiAndConfig = new EcardApiBusiAndConfig();
        ecardBusiAndConfig.setBusiId(ecardBusiReportPerson.getId());
        ecardBusiAndConfig.setBusiType(1);//业务填报分类：1-个人类，2-绑卡类，3-对公类，4-其他业务
        ecardBusiAndConfig.setConfigId(Long.valueOf(configId));
        ecardBusiAndConfig.setIntegralNums(points);
        ecardBusiAndConfigList.add(ecardBusiAndConfig);
        //批量插入业务填报信息和业务类型关联表数据
        ecardBusiAndConfigMapper.batchAddEcardBusiAndConfigInfo(ecardBusiAndConfigList);
        return i;
    }

    /**
     * 条件查询个人类业务列表
     * @param ecardBusiReportPerson
     * @return
     */
    @Override
    public List<EcardApiBusiReportPerson> getMyReportPersonList(EcardApiBusiReportPerson ecardBusiReportPerson, String salt) {
        List<EcardApiBusiReportPerson> busiReportPersonList = ecardBusiReportPersonMapper.getMyReportPersonList(ecardBusiReportPerson);
        if (busiReportPersonList.size() > 0) {
            for (EcardApiBusiReportPerson busiReportPerson : busiReportPersonList) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(busiReportPerson.getPerCustPhone())) {
                        busiReportPerson.setPerCustPhone(AESEncryptionUtils.decrypt(busiReportPerson.getPerCustPhone(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(busiReportPerson.getPerCustIdcard())) {
                        busiReportPerson.setPerCustIdcard(AESEncryptionUtils.decrypt(busiReportPerson.getPerCustIdcard(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return busiReportPersonList;
    }
}
